import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/main/entity/commo_spec4app_record_entity.dart';
import 'package:shopping_group/utils/route_util.dart';

class MoreMoneyJuanBloc implements BlocBase {
  List<CommoSpec4AppRecordEntity> _listEntity = [];

  StreamController<Object> _initCommSpecController = StreamController<Object>();
  StreamSink<Object> get initCommSpecData => _initCommSpecController.sink;

  StreamController<Object> _commSpecController = StreamController<Object>();
  StreamSink<Object> get _addCommSpecData => _commSpecController.sink;
  Stream<Object> get sendCommSpecData => _commSpecController.stream;

  MoreMoneyJuanBloc() {
    _initCommSpecController.stream.listen(commSpecData);
  }

  void commSpecData(data) {
    _listCommSpecTask(1);
  }

  Future _listCommSpecTask(pageNo) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    data['val'] = BaseConstant.MONEY_TYPE;
    data['pageIndex'] = pageNo;
    data['pageSize'] = BaseConstant.PAGE_SIZE;
    HttpUtilAll.post(Apis.AUC_COMMO_URL, data: data, headers: {},
        success: (result) {
      _listEntity?.clear();
      // 请求成功返回的数据
      if (null != result) {
        List list = List.from(result);
        for (var item in list) {
          CommoSpec4AppRecordEntity params =
              CommoSpec4AppRecordEntity.fromJson(item);
          _listEntity.add(params);
        }
      }
      _addCommSpecData.add(_listEntity);
      // eventBus.fire(CommoSpecEvent(_listEntity, _type));
    }, error: (errorMsg) {
      // 请求失败返回的错误信息
      BotToast.showText(text: errorMsg);
    });
  }

  @override
  void dispose() {
    _initCommSpecController?.close();
    _commSpecController?.close();
  }
}
